package com.mall.web.control.app.code;

import java.awt.image.BufferedImage;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.code.kaptcha.Producer;
import com.mall.client.user.InviteCodeService;
import com.mall.pojo.InviteCode;
import com.mall.web.util.WebUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/captcha/")
public class CaptchaController {
	@Autowired
	private Producer producer;

	@Autowired
	private InviteCodeService inviteCodeService;

	@RequestMapping("code")
	public void kaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/jpeg");

		String capText = producer.createText();
		String sessionId = WebUtil.getSessionId(request);
//		String jUsessionid = getJUSESSIONID(request);


//		if (StringUtils.isBlank(jUsessionid)) {


//			jUsessionid = createJUSESSIONID(sessionId, null);


//			setJUSESSIONID(request, response, sessionId, null);


//		}


		InviteCode inviteCode = this.inviteCodeService.getInviteCodeBySessionIdAndType1(sessionId);
		if (inviteCode!=null) {
			inviteCode.setCode(capText);
			inviteCode.setGmtModify(new Date());
			this.inviteCodeService.modifyByPic4InviteCode(inviteCode);
		}else {
			inviteCode = new InviteCode();
			inviteCode.setCode(capText);
			inviteCode.setPhone(sessionId);
			inviteCode.setGmtCreate(new Date());
			inviteCode.setGmtModify(new Date());
			inviteCode.setStatus(0);
			inviteCode.setType(1);
			inviteCode.setEffectiveTime(new Date());
			this.inviteCodeService.insertInviteCode(inviteCode);
		}


		BufferedImage image = producer.createImage(capText);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		try {
			out.flush();
		} finally {
			out.close();
		}
	}

	@RequestMapping("codenumForAndroid")
	@ResponseBody
	public ResponseEntity<InviteCode> codenumForAndroid(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		//response.setContentType("image/jpeg");

		String capText = producer.createText();
		String sessionId = WebUtil.getSessionId(request);



		InviteCode inviteCode = this.inviteCodeService.getInviteCodeBySessionIdAndType1(sessionId);
		if (inviteCode!=null) {
			inviteCode.setCode(capText);
			inviteCode.setGmtModify(new Date());
			this.inviteCodeService.modifyByPic4InviteCode(inviteCode);
		}else {
			inviteCode = new InviteCode();
			inviteCode.setCode(capText);
			inviteCode.setPhone(sessionId);
			inviteCode.setGmtCreate(new Date());
			inviteCode.setGmtModify(new Date());
			inviteCode.setStatus(0);
			inviteCode.setType(1);
			inviteCode.setEffectiveTime(new Date());
			this.inviteCodeService.insertInviteCode(inviteCode);
		}
		ResponseEntity<InviteCode> responseEntity = new ResponseEntity<>();
		responseEntity.setData(inviteCode);
		responseEntity.setCode("200");
		responseEntity.setMessage("yanzhengma");
		return responseEntity;

		
	}
//	private String getJUSESSIONID(HttpServletRequest request) {


//		String cookieValue = "";


//		Cookie[] cookies = request.getCookies();// 这样便可以获取一个cookie数组


//		if (cookies == null) {


//			return "";


//		}


//		for (Cookie cookie : cookies) {


//			if (cookie.getName().equalsIgnoreCase("JUSESSIONID")) {


//				cookieValue = cookie.getValue();


//				return cookieValue;


//			}


//		}


//		return cookieValue;


//	}


//


//	private void setJUSESSIONID(HttpServletRequest request, HttpServletResponse response, String sessionId,


//			Long userId) {


//		Cookie cookie2 = new Cookie("JUSESSIONID", createJUSESSIONID(sessionId, userId));


//		cookie2.setPath("/");


//		cookie2.setMaxAge(3600 * 24 * 30);


//		response.addCookie(cookie2);


//	}


//


//	private String createJUSESSIONID(String sessionId, Long userId) {


//		return sessionId;


//	}


}
